package com.xgimi.open.skill.datamanager.biz.utils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.GZIPInputStream;

public class TextFileReader {

    public static void main(String[] args) {

        String file = "data/treewords.txt";
        List<String> result = TextFileReader.readLines(file);

        System.out.println("ok" + result);
    }

    public static List<String> readZipLines(String zipFileName) {

        List<String> arrayList = new ArrayList<String>();

        try (InputStream fileStream = new FileInputStream(zipFileName);
             InputStream gzipStream = new GZIPInputStream(fileStream);
             Reader decoder = new InputStreamReader(gzipStream, "UTF-8");
             BufferedReader bufferedReader = new BufferedReader(decoder);) {
            String arrayListString = null;
            while ((arrayListString = bufferedReader.readLine()) != null) {
                if (arrayListString.equals("")) {
                    continue;
                }
                arrayList.add(arrayListString);
            }
            bufferedReader.close();
            fileStream.close();
            gzipStream.close();
            decoder.close();
        } catch (Exception e) {
        }
        return arrayList;
    }

    public static HashSet<String> readUniqueLines(InputStream is) {

        HashSet<String> terms = new HashSet<String>();
        if (is == null) {
            return terms;
        }

        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(is, "utf-8"));
            String str = null;

            while ((str = bufferedReader.readLine()) != null) {

                if (!str.trim().equals("") && !str.startsWith("#") && !terms.contains(str)) {
                    terms.add(str);
                }
            }
        } catch (Exception e) {
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
            }
        }

        return terms;
    }

    public static List<String> readLines(String fileName) {
        List<String> arrayList = new ArrayList<String>();

        File file = new File(fileName);
        InputStream is = null;

        BufferedReader bufferedReader = null;
        try {
            if (!file.exists()) {
                is = TextFileReader.class.getClassLoader().getResourceAsStream(fileName);
            } else {
                is = new FileInputStream(file);
            }
            try {
                InputStreamReader isr = new InputStreamReader(is, "utf-8");
                bufferedReader = new BufferedReader(isr);
            } catch (NullPointerException e) { //文件不存在
                return arrayList;
            }

            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                if (line.startsWith("#")) {
                    continue;
                }
                if (line.trim().equals("")) {
                    //continue;
                }
                arrayList.add(line);

            }

            bufferedReader.close();

        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                }
            }
        }
        return arrayList;
    }

    /**
     * 去空行，去重，忽略“#”开头的行
     *
     * @param fileName
     * @return
     */
    public static Set<String> readUniqueLines(String fileName) {

        File file = new File(fileName);
        InputStream is = null;

        if (!file.exists()) {
            is = TextFileReader.class.getClassLoader().getResourceAsStream(fileName);
        } else {
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
            }
        }

        return readUniqueLines(is);
    }
}
